package com._520it.crm.service.impl;

import com._520it.crm.domain.Client;
import com._520it.crm.mapper.ClientMapper;
import com._520it.crm.mapper.OrderBillMapper;
import com._520it.crm.page.PageResult;
import com._520it.crm.query.QueryObject;
import com._520it.crm.service.IClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * Created by 呆 on 2017/10/3.
 */
//业务里欠缺employee录入人

@Service
public class ClientServiceImpl implements IClientService {
    @Autowired
    private ClientMapper clientMapper;
    @Autowired
    private OrderBillMapper orderBillMapper;
    @Override
    public int deleteByPrimaryKey(Long id) {
        return clientMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Client client) {
        client.setInputTime(new Date());
        return clientMapper.insert(client);
    }

    @Override
    public Client selectByPrimaryKey(Long id) {
        return clientMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Client> selectAll() {
        return clientMapper.selectAll();
    }

    @Override
    public int updateByPrimaryKey(Client client) {

        return clientMapper.updateByPrimaryKey(client);
    }

    public int updateByPrimaryClient(Client client) {

        client.setEditTime(new Date());

        return clientMapper.updateByPrimaryClient(client);
    }

    @Override
    public PageResult query(QueryObject qo) {
        Integer totalCount = clientMapper.queryforCount(qo);
        if (totalCount==0){
            return new PageResult(0, Collections.EMPTY_LIST);
        }
        List<Client> data = clientMapper.queryforList(qo);
        return new PageResult(totalCount,data);
    }

    @Override
    public void selectClient(Client client,Long orderBillId) {
        Client clientExist=clientMapper.selectClient(client.getIdCardType(),client.getIdCardNumber());
        if (clientExist==null){
            clientMapper.insert(client);
            //orderBillMapper.updataApplyClientIdCardNumber(orderBillId,client.getIdCardNumber());
        }else {
            //orderBillMapper.updataApplyClientIdCardNumber(orderBillId,client.getIdCardNumber());
            int i = 1/0;
        }
    }

    @Override
    public void saveRecognizee(Client client, Long orderBillId) {
        Client clientExist=clientMapper.selectClient(client.getIdCardType(),client.getIdCardNumber());
        if (clientExist==null){
            clientMapper.insert(client);
            orderBillMapper.updataRecognizeeIdCardNumber(orderBillId,client.getIdCardNumber());
        }else {
            orderBillMapper.updataRecognizeeIdCardNumber(orderBillId,client.getIdCardNumber());
        }
    }

    @Override
    public Client selectApplyClient(Long id) {
        return clientMapper.selectApplyClient(id);
    }

    @Override
    public Client selectRecognizee(Long id) {
        return clientMapper.selectRecognizee(id);
    }

    @Override
    public Client selectCarOwner(Long id) {
        return clientMapper.selectCarOwner(id);
    }



    /**
     * 潜在客户转为正式客户的业务方法
     * */
    @Override
    public void changeFormalClientById(Long id) {

        clientMapper.changeFormalClientById(id,Client.FORMAL_CLIENT);
        Client client = clientMapper.selectByPrimaryKey(id);
        client.setEditTime(new Date());
        clientMapper.updateByPrimaryKey(client);

    }
    /**
     * 潜在客户转为资源池客户的业务方法
     * */
    @Override
    public void changeSourceFormalClientById(Long id) {
        clientMapper.changeFormalClientById(id,Client.SOURCE_CLIENT);
        Client client = clientMapper.selectByPrimaryKey(id);
        client.setEditTime(new Date());
        clientMapper.updateByPrimaryKey(client);

    }


    @Override
    public PageResult employeeQuery(QueryObject qo) {
        Integer totalCount = clientMapper.employeeQueryforCount(qo);
        if (totalCount==0){
            return new PageResult(0, Collections.EMPTY_LIST);
        }
        List<Client> data = clientMapper.employeeQueryforList(qo);
        return new PageResult(totalCount,data);
    }
}
